package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @className CheckGroupServiceImpl
 */
@Service
@Transactional
@RequiredArgsConstructor
public class CheckGroupServiceImpl implements CheckGroupService {

    private final CheckGroupMapper checkGroupMapper;
    private final CheckItemMapper checkItemMapper;
    private final SetMealMapper setMealMapper;

    /**
     * 新增检查组
     * @param checkItemIds
     * @param checkGroup
     * @return void
     **/
    public void addCheckGroup(List<Integer> checkItemIds, CheckGroup checkGroup) {
        checkGroupMapper.insertCheckGroup(checkGroup); // 先添加检查组
        if (!CollectionUtils.isEmpty(checkItemIds)) {
            checkGroupMapper.insertGroupCheckItemRelation(checkGroup.getId(), checkItemIds); // 再添加检查项和检查组关联
        }
    }

    /**
     * 删除检查组
     * @param groupId
     * @return void
     **/
    public void deleteCheckGroupById(Integer groupId) {
        checkGroupMapper.deleteGroupCheckItemRelation(groupId); //先删除检查组和检查项关联
        setMealMapper.deleteSetMealAndCheckGroupRelation(groupId); //再删除套餐和检查组关联
        checkGroupMapper.deleteCheckGroupById(groupId); //再删除检查组
    }

    /**
     * 分页查询检查组
     * @param queryPageBean 分页条件
     * @return PageResult
     **/
    public PageResult getCheckGroupByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()); //开始分页
        List<CheckGroup> checkGroupList = checkGroupMapper.selectCheckGroupByPage(queryPageBean.getQueryString());
        return new PageResult(new PageInfo(checkGroupList));
    }

    /**
     * 编辑检查组
     * @param checkItemIds 检查项id集合
     * @param checkGroup 检查组对象
     * @return Result
     **/
    public void editCheckGroupById(List<Integer> checkItemIds, CheckGroup checkGroup) {
        checkGroupMapper.updateCheckGroup(checkGroup); // 先修改检查组
        checkGroupMapper.deleteGroupCheckItemRelation(checkGroup.getId()); // 再删除关联关系
        if (!CollectionUtils.isEmpty(checkItemIds)) {
            checkGroupMapper.insertGroupCheckItemRelation(checkGroup.getId(), checkItemIds); // 再添加检查项和检查组关联
        }
    }

    /**
     * 根据检查组id查询检查组
     * @param groupId 检查组id
     * @return Result
     **/
    public CheckGroup getCheckGroupById(Integer groupId) {
        CheckGroup checkGroup = checkGroupMapper.selectCheckGroupById(groupId); //查询检查组基本信息
        List<Integer> itemIds = checkItemMapper.selectCheckItemIdsByGroupId(groupId); //查询关联的检查项ID列表
        if (!CollectionUtils.isEmpty(itemIds)) {
            List<CheckItem> checkItems = checkItemMapper.selectCheckItemsByIds(itemIds); //查询所属检查项详情
            checkGroup.setCheckItems(checkItems);
        }
        return checkGroup;
    }


    /**
     * 根据套餐id查询套餐所对应的检查组id集合
     * @param setMealId 套餐id
     * @return List<Integer>
     **/
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setMealId) {
        return checkGroupMapper.selectCheckGroupIdsBySetmealId(setMealId);
    }

    /**
     * 查询所有检查组
     * @return List<CheckGroup>
     **/
    public List<CheckGroup> getAllCheckGroup() {
        return checkGroupMapper.selectAllCheckGroup();
    }

}
